﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace INHOADON_TAMVAN.Chuc_Nang
{
    public partial class frmEditHoaDon : DevExpress.XtraEditors.XtraForm
    {
        public frmEditHoaDon()
        {
            InitializeComponent();
        }
        public delegate void reloadListEdit();
        public reloadListEdit reloadList;

        DataClasses1DataContext db = new DataClasses1DataContext(SystemInfo.pub_ConnString);

        //get id
        public string id_hoadon;
        
      

        private void btnSaveHoaDon_Click(object sender, EventArgs e)
        {
            this.save(id_hoadon);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            textEditHoTenKhachHang.Text = "";
            textEditDonVi.Text = "";
            textEditDiaChi.Text = "";
            textEditHinhThucThanhToan.Text = "";
            textEditThueSuat.Text = "";
            textEditMaSoThue.Text = "";
            textEditSoTaiKhoan.Text = "";
        }

        private void btnLuuChiTiet_Click(object sender, EventArgs e)
        {
            this.loadGC();
            db.SubmitChanges();
        }

        private void btnXuatHoaDon_Click(object sender, EventArgs e)
        {

        }

        private void frmEditHoaDon_Load(object sender, EventArgs e)
        {
            if (id_hoadon == null)
            {
                XtraMessageBox.Show("Vui lòng chọn một hóa đơn để sửa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.loadList();
                this.loadGC();
            }
        }
        private void loadList()
        {
            var hoadon = from hd in db.HoaDons where hd.HD_ID == id_hoadon select hd;
            foreach (HoaDon hd in hoadon)
            {
                textEditSoHoaDon.Text = hd.HD_ID;
                textEditDiaChi.Text = hd.KhachHang.KH_DIACHI;
                textEditHoTenKhachHang.Text = hd.KhachHang.KH_HOTEN;
                textEditDonVi.Text = hd.KhachHang.KH_DONVI;
                textEditHinhThucThanhToan.Text = hd.HD_Hinhthucthanhtoan;
                textEditThueSuat.Text = hd.HD_Thuesuat.ToString();
                textEditSoTaiKhoan.Text = hd.KhachHang.KH_STK;
                textEditMaSoThue.Text = hd.KhachHang.KH_MASOTHUE;
                dateEditNgayHD.Text = hd.HD_Ngayhd.ToString();
            }
        }
        private void save( string id)
        {
            try
            {
                var hoadon = (from hd in db.HoaDons where hd.HD_ID == id_hoadon select hd).Single();
                hoadon.HD_ID = textEditSoHoaDon.Text;
                hoadon.KhachHang.KH_HOTEN = textEditHoTenKhachHang.Text;
                hoadon.HD_Ngayhd = DateTime.Parse(dateEditNgayHD.Text);
                hoadon.KhachHang.KH_DONVI = textEditDonVi.Text;
                hoadon.KhachHang.KH_DIACHI = textEditDiaChi.Text;
                hoadon.HD_Thuesuat = double.Parse(textEditThueSuat.Text);
                hoadon.KhachHang.KH_MASOTHUE = textEditMaSoThue.Text;
                hoadon.KhachHang.KH_STK = textEditSoTaiKhoan.Text;
                hoadon.HD_Hinhthucthanhtoan = textEditHinhThucThanhToan.Text;
                db.SubmitChanges();
                XtraMessageBox.Show("Lưu thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                textEditDiaChi.Enabled = false;
                textEditHoTenKhachHang.Enabled = false;
                textEditDonVi.Enabled = false;
                textEditHinhThucThanhToan.Enabled = false;
                textEditThueSuat.Enabled = false;
                textEditSoTaiKhoan.Enabled = false;
                textEditMaSoThue.Enabled = false;
                dateEditNgayHD.Enabled = false;


                gcEditHoaDon.Enabled = true;
                btnLuuChiTiet.Enabled = true;
                btnXuatHoaDon.Enabled = true;

            }
            catch (Exception)
            {
                throw;
            }
        }
        private void loadGC()
        {
            var chitiet = from cthd in db.Chitiethoadons join hanghoa in db.HangHoas on cthd.CTHD_HH_ID equals hanghoa.HH_ID where cthd.CTHD_HD_ID == id_hoadon select new { hanghoa.HH_Tenhanghoa, cthd.CTHD_Soluong, hanghoa.HH_Dongia, cthd.CTHD_ThanhTien, hanghoa.HH_DonViTinh };
            gcEditHoaDon.DataSource = chitiet;
        }

        private void gvEditHoaDon_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }
    }
}